package com.gun.management.module.raffle.controller.app;

import com.gun.management.framework.common.exception.ServiceException;
import com.gun.management.framework.common.pojo.CommonResult;
import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.common.util.object.BeanUtils;
import com.gun.management.framework.security.core.LoginUser;
import com.gun.management.module.member.api.user.MemberUserApi;
import com.gun.management.module.member.api.user.dto.MemberUserRespDTO;
import com.gun.management.module.raffle.controller.app.vo.*;
import com.gun.management.module.raffle.service.lotterylog.LotteryBizService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import static com.gun.management.framework.common.pojo.CommonResult.success;
import static com.gun.management.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
import static com.gun.management.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;


@Tag(name = "用户 APP - 抽奖")
@RestController
@RequestMapping("/raffle/lottery")
@Validated
public class AppLotteryController {

    @Resource
    private LotteryBizService lotteryBizService;

    @Resource
    private MemberUserApi memberUserApi;

    @GetMapping("/getLotteryPrizeList")
    @Operation(summary = "抽奖活动列表")
    // @PreAuthorize("@ss.hasPermission('raffle:lottery:getLotteryPrizeList')")
    public CommonResult<AppLotteryRespVO>  getLotteryPrizeList(@Valid AppLotteryCategoryReqVO reqVO) {
        LoginUser loginUser = getLoginUser();
        MemberUserRespDTO user = memberUserApi.getUser(getLoginUserId());
        if (user == null) throw new ServiceException(777,"登录信息已失效,请重新登录");

        return success(BeanUtils.toBean(lotteryBizService.getLotteryPrizeList(loginUser.getTenantId(),reqVO,user.getId()), AppLotteryRespVO.class));
    }

    @GetMapping("/getAllUsersLotteryLogPage")
    @Operation(summary = "活动中奖记录")
    // @PreAuthorize("@ss.hasPermission('raffle:lottery:getAllUsersLotteryLogPage')")
    public CommonResult<PageResult<AppLotteryLogRespVO>> getAllUsersLotteryLogPage(@Valid AppLotteryLogPageReqVO pageReqVO) {
        LoginUser loginUser = getLoginUser();
        MemberUserRespDTO user = memberUserApi.getUser(getLoginUserId());
        if (user == null) throw new ServiceException(777,"登录信息已失效,请重新登录");

        return success(BeanUtils.toBean(lotteryBizService.getAppLotteryLogPageList(loginUser.getTenantId(),pageReqVO,user.getId()), AppLotteryLogRespVO.class));
    }


    @GetMapping("/getUsersLotteryLogPage")
    @Operation(summary = "用户中奖记录")
    // @PreAuthorize("@ss.hasPermission('raffle:lottery:getUsersLotteryLogPage')")
    public CommonResult<PageResult<AppUserLotteryLogRespVO>> getUsersLotteryLogPage(@Valid AppLotteryLogPageReqVO pageReqVO) {
        LoginUser loginUser = getLoginUser();
        MemberUserRespDTO user = memberUserApi.getUser(getLoginUserId());
        if (user == null) throw new ServiceException(777,"登录信息已失效,请重新登录");

        return success(BeanUtils.toBean(lotteryBizService.getAppUserLotteryLogPageList(loginUser.getTenantId(),pageReqVO), AppUserLotteryLogRespVO.class));
    }

    @GetMapping("/getGroupPageList")
    @Operation(summary = "中奖记录日期分组")
    public CommonResult<PageResult<AppYearMonthGroupUserLotteryLogRespVO>> getGroupPageList(@Valid AppLotteryLogPageReqVO pageReqVO) {
        LoginUser loginUser = getLoginUser();
        MemberUserRespDTO user = memberUserApi.getUser(getLoginUserId());
        if (user == null) throw new ServiceException(777,"登录信息已失效,请重新登录");

        return success(BeanUtils.toBean(lotteryBizService.getYearMonthGroupLotteryLogList(loginUser.getTenantId(),pageReqVO),AppYearMonthGroupUserLotteryLogRespVO.class));
    }

    @GetMapping("/getPlayResult")
    @Operation(summary = "中奖结果查询")
   // @PreAuthorize("@ss.hasPermission('raffle:lottery:getPlayResult')")
    public  CommonResult<AppLotteryPrizeRetVO> getPlayResult(@Valid AppLotteryReqVO reqVO) {
        LoginUser loginUser = getLoginUser();

        MemberUserRespDTO user = memberUserApi.getUser(getLoginUserId());
        if (user == null) throw new ServiceException(777,"登录信息已失效,请重新登录");


        AppLotteryPrizeRespVO appLotteryPrizeRespVO =  lotteryBizService.getPlayResult(loginUser.getTenantId(),reqVO,user.getId());

        if (appLotteryPrizeRespVO == null) throw new ServiceException(601,"系统或网络异常！");

        AppLotteryPrizeRetVO appLotteryPrizeRetVO = new AppLotteryPrizeRetVO();
        appLotteryPrizeRetVO.setLotteryLogId(appLotteryPrizeRespVO.getLotteryLogId()); // 中奖记录ID
        appLotteryPrizeRetVO.setName(appLotteryPrizeRespVO.getName()); // 奖品名称
        appLotteryPrizeRetVO.setPrizeCategory(appLotteryPrizeRespVO.getPrizeCategory()); // 奖品类别
        appLotteryPrizeRetVO.setAvatar(appLotteryPrizeRespVO.getAvatar()); // 奖品头像
        appLotteryPrizeRetVO.setPrompt(appLotteryPrizeRespVO.getPrompt()); // 提示语
        appLotteryPrizeRetVO.setSku(appLotteryPrizeRespVO.getSku()); // Sku
        appLotteryPrizeRetVO.setProductId(appLotteryPrizeRespVO.getProductId()); // 添加产品ID
        appLotteryPrizeRetVO.setPrizeId(appLotteryPrizeRespVO.getPrizeId()); // 添加奖品ID
        appLotteryPrizeRetVO.setSkuId(appLotteryPrizeRespVO.getSkuId()); // 添加Sku商品ID

        return success(appLotteryPrizeRetVO);
    }


    @GetMapping("/getRecvPrizeResult")
    @Operation(summary = "立即领取")
    // @PreAuthorize("@ss.hasPermission('raffle:lottery:getRecvPrizeResult')")
    public  CommonResult<AppLotteryRecvPrizeRespVO> getRecvPrizeResult(@Valid AppLotteryRecvPrizeReqVO reqVO) {
        LoginUser loginUser = getLoginUser();
        MemberUserRespDTO user = memberUserApi.getUser(getLoginUserId());
        if (user == null) throw new ServiceException(777,"登录信息已失效,请重新登录");

        return success(BeanUtils.toBean(lotteryBizService.getRecvPrizeResult(loginUser.getTenantId(),reqVO,user.getId()), AppLotteryRecvPrizeRespVO.class));
    }

    @GetMapping("/getVerifyResult")
    @Operation(summary = "抽奖校验")
    public  CommonResult<Boolean> getVerifyResult(@RequestParam("lotteryId") long lotteryId) {
        MemberUserRespDTO user = memberUserApi.getUser(getLoginUserId());
        if (user == null) throw new ServiceException(777,"登录信息已失效,请重新登录");

        return success(lotteryBizService.getVerifyResult(lotteryId,user.getId())).setMsg("抽奖校验已通过");
    }

}
